.TH std::move_iterator::operator*,-> 3 "2024.06.10" "http://cppreference.com" "C++ Standard Libary"
.SH NAME
std::move_iterator::operator*,-> \- std::move_iterator::operator*,->

.SH Synopsis
   reference operator* () const; \fB(1)\fP \fI(since C++11)\fP
                                     (constexpr since C++17)
                                     \fI(since C++11)\fP
   pointer operator->() const;   \fB(2)\fP (constexpr since C++17)
                                     (deprecated in C++20)

   Returns an rvalue-reference or pointer to the current element.

   1) Equivalent to
   static_cast<reference>(*base())
   \fI(until C++20)\fP
   ranges::iter_move(base())
   \fI(since C++20)\fP.
   2) Equivalent to base().

.SH Parameters

   \fI(none)\fP

.SH Return value

   1) Rvalue-reference to the current element or its copy.
   2) Copy of the underlying iterator. A pointer to the current element is eventually
   returned if -> is directly used.

.SH Notes

   Note that \fB(2)\fP eventually returns a pointer if -> is directly used. When
   dereferencing a pointer the returned value is an lvalue. This may lead to unintended
   behavior.

.SH Example


// Run this code

 #include <iomanip>
 #include <iostream>
 #include <iterator>
 #include <string>
 #include <vector>

 void print(auto rem, auto const& v)
 {
     for (std::cout << rem; auto const& e : v)
         std::cout << std::quoted(e) << ' ';
     std::cout << '\\n';
 }

 int main()
 {
     std::vector<std::string> p{"alpha", "beta", "gamma", "delta"}, q;
     print("1) p: ", p);
     for (std::move_iterator it{p.begin()}, end{p.end()}; it != end; ++it)
     {
         it->push_back('!'); // calls -> string::push_back(char)
         q.emplace_back(*it); // *it <- overload (1)
     }
     print("2) p: ", p);
     print("3) q: ", q);

     std::vector v{1, 2, 3};
     std::move_iterator it{v.begin()};
     // *it = 13; // error: using rvalue as lvalue
 }

.SH Possible output:

 1) p: "alpha" "beta" "gamma" "delta"
 2) p: "" "" "" ""
 3) q: "alpha!" "beta!" "gamma!" "delta!"

   Defect reports

   The following behavior-changing defect reports were applied retroactively to
   previously published C++ standards.

      DR    Applied to             Behavior as published              Correct behavior
                       dereferencing a move_iterator could return a
   LWG 2106 C++11      dangling reference                            returns the object
                       if the dereferencing the underlying iterator  instead
                       returns a prvalue

.SH See also

   operator[] accesses an element by index
   \fI(C++11)\fP    \fI(public member function)\fP
